<?php

declare(strict_types=1);

namespace app\common\model;

use think\Model;
use think\response\Json;

/**
 * @mixin \think\Model
 */
class Pro extends Base
{

    protected $name = 'pro';

    public static function proSelect()
    {
        parent::startTrans();
        try {
            $find = parent::where(['is_del' => 0])->order('sort asc')->field('id,title')->select()->toArray();
            if (empty($find)) {
                return [];
            }
            foreach ($find as $k => $v) {
                $data[$v['id']] = $v['title'];
            }
            return  $data;
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


    public static function proSelectTable()
    {
        parent::startTrans();
        try {
            $data = parent::where(['is_del' => 0])->order('sort asc')->field('id,title')->select()->toArray();
            return  $data;
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 报告生成
     */
    public static function pdfShow($param)
    {
        try {
            $checkReport = CheckReport::where(['id' => $param['id']])->find();
            $CheckList = CheckList::where(['id' => $checkReport['check_list_id'], 'is_del' => 0])->find();
            if (empty($CheckList)) {
                return json(['code' => -1, 'msg' => '还未进行检查，没有可输出的文件！']);
            }
            $company = Company::where(['id' => $CheckList['company_id'], 'is_del' => 0])->find();
            $district = District::where(['id' => $company['district_id'], 'is_del' => 0])->find();
            $company['pro_name'] = DistrictPro::where(['id' => $company['place_id'], 'is_del' => 0])->value('title');
            $company['imageslist'] = explode(',', $company['images']);

            if ($CheckList['one_status'] == 1) {
                $company['statusname'] = '未检';
                $company['statuslistname'] = '未选择';
            } else {
                $company['statusname'] = '已检/' . User::where(['id' => $CheckList['one_uid'], 'is_del' => 0])->value('nickname');
                if ($company['status'] == 0) {
                    $names = '正常';
                } else if ($company['status'] == 1) {
                    $names = '倒闭';
                } else if ($company['status'] == 2) {
                    $names = '已搬迁';
                } else if ($company['status'] == 3) {
                    $names = '责令停工停产';
                } else if ($company['status'] == 4) {
                    $names = '装修中/未营业';
                } else {
                    $names = '台账重复';
                }
                $company['statuslistname'] = $names;
            }
            //分类列
            $child = ProTopic::where(['pro_id' => $CheckList['pro_id'], 'is_del' => 0])->field('id,title,pro_id,cate_id,desc,score,sort')->order('sort asc')->select();
            foreach ($child as $kk => $vv) {
                $checkDetail = CheckListDetails::where(['is_del' => 0, 'check_list_id' => $CheckList['id'], 'topic_id' => $vv['id'], 'district_id' => $CheckList['district_id']])->find();
                if ($checkDetail) {
                    $vv['status'] = 1;
                    $vv['checkDetail'] = $checkDetail;
                } else {
                    $vv['status'] = 0;
                    $vv['checkDetail'] = '';
                }
                $child[$kk] = $vv;
            }
            ///隐患列表
            $dangerList = CheckListDetails::where(['district_id' => $CheckList['district_id'], 'company_id' => $CheckList['company_id'], 'check_list_id' => $CheckList['id'], 'is_del' => 0])->order('topic_id asc')->select();
            if ($dangerList) {
                foreach ($dangerList as $k => $v) {
                    $v['topicname'] = ProTopic::where(['id' => $v['topic_id'], 'is_del' => 0])->value('title');
                    $dangerList[$k] = $v;
                }
            }
            $dangerCount = CheckListDetails::where(['district_id' => $CheckList['district_id'], 'company_id' => $CheckList['company_id'], 'check_list_id' => $CheckList['id'],  'is_del' => 0])->count();
            $check_desc = '不符合描述';
            $check_zg = '整改图片';
            $check_yh = '隐患图片';
            if ($checkReport['types'] == 4) {
                $title = '消防安全隐患清单';
            } else if ($checkReport['types'] == 5) {
                $title = '消防安全隐患复查清单';
            } else {
                $title = !empty($district['check_title']) ? $district['check_title'] : '消防安全检查记录';
                $check_desc = !empty($district['check_desc']) ? $district['check_desc'] : '不符合描述';
                $check_zg = !empty($district['check_zg']) ? $district['check_zg'] : '整改图片';
                $check_yh = !empty($district['check_yh']) ? $district['check_yh'] : '隐患图片';
            }

            $score = $CheckList['one_score'];
            $sign = $CheckList['one_uid_sign'];
            $companySign = $CheckList['one_company_sign'];

            $pdf = new \TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
            // 设置文档信息
            $pdf->SetTitle($title);
            $pdf->SetHeaderData('', 0, '', "", array(255, 255, 255), array(255, 255, 255));
            $pdf->setFooterData(array(0, 0, 0), array(0, 0, 0));
            $pdf->setHeaderFont(array('stsongstdlight', '', '12'));
            $pdf->setFooterFont(array('helvetica', '', '8'));
            $pdf->SetDefaultMonospacedFont('courier');
            $pdf->SetFooterMargin(10);
            $pdf->SetAutoPageBreak(TRUE, 25);
            $pdf->setImageScale(1);
            $pdf->setFontSubsetting(true);
            $pdf->SetAutoPageBreak(TRUE, '15');
            $pdf->setCellHeightRatio(1.75);
            $pdf->SetAutoPageBreak(true, '20');
            $pdf->AddPage();
            $create_time = $CheckList['create_time'];
            $html = '
             <style>
               .yhtitle{width:20%;height:40px;line-height:40px;border-bottom:1px solid #333; border-right:1px solid #333;}
               .yhnr{width:30%;height:40px;line-height:40px;border-bottom:1px solid #333; border-right:1px solid #333;}
               .trtitle{width:20%;height:40px; line-height:40px; border-bottom:1px solid #333;}
               .trcontent{width:40%;height:40px;line-height:40px; border:1px solid #333; border-top:none;}
               .address{ font-size:12px;}
               .imsgestd{height:240px;position: relative; }
               .yimages{width:210px; height:135px; display:block; float:left; }
               .detailimg{width:100px; height:75px; display:block; float:left;}
               .tdh2{ width:100%; line-height:60px; font-weight:800; font-size:20px;}
               .trid{ width:5%; border-bottom:1px solid #333; border-right:1px solid #333; }
               .thstatus{ width:15%;border-bottom:1px solid #333; border-right:1px solid #333;}
               .thtitle{ width:50%; border-bottom:1px solid #333; border-right:1px solid #333; font-size:13px;}
               .thdesc{ width:30%; border-bottom:1px solid #333; border-right:1px solid #333;}
               .thdescyh{ width:45%; border-bottom:1px solid #333; border-right:1px solid #333;}
               .thdescyh1{ width:22.5%; border-bottom:1px solid #333; border-right:1px solid #333;}
               .thdescyh2{ width:45%; border-bottom:1px solid #333; border-right:1px solid #333;}
               .red{ color:red;}
             </style>';

            $html .= '<table  align="centter" style="width=100%;">';
            $html .= '<tr>';
            $html .= '<td class="tdh2">' . $title . '</td>';
            $html .= '</tr>';
            $html .= '<table  align="left" style="width=100%; height:300px; border:1px solid #333;"  valign="middle">';
            $html .= '<tr>';
            $html .= '<td class="trtitle">场所类别</td>';
            $html .= '<td class="trcontent">' . $company['pro_name'] . ' </td>';
            $html .= '<td class="trcontent"> 营业执照或场所图片</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">场所/单位名</td>';
            $html .= '<td class="trcontent">' . $company['company_name'] . '</td>';
            $html .= '<td class="trcontent imsgestd" rowspan="7">';
            $html .= '<img src="' . $company['imageslist'][0] . '" class="yimages" /><br/>';
            if (!empty($company['imageslist'][1])) {
                $html .= '<img src="' . $company['imageslist'][1] . '" class="yimages" />';
            }
            $html .= '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">地址</td>';
            $html .= '<td class="trcontent address" >' . $company['address'] . '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">经营面积</td>';
            $html .= '<td class="trcontent">' . $company['area'] . '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">建筑高度</td>';
            $html .= '<td class="trcontent">' . $company['height'] . '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">消防负责人</td>';
            $html .= '<td class="trcontent">' . $company['fzname'] . '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">联系电话</td>';
            $html .= '<td class="trcontent">' . $company['lxtel'] . '</td>';
            $html .= '</tr>';
            $html .= '<tr>';
            $html .= '<td class="trtitle">检查日期</td>';
            $html .= '<td class="trcontent">' . $create_time . '</td>';
            $html .= '</tr>';
            // $html .= '<tr>';
            // $html .= '<td class="trtitle">检查人</td>';
            // $html .= '<td class="trcontent">' . $company['statusname'] . '</td>';
            // $html .= '</tr>';
            // $html .= '<tr>';
            // $html .= '<td class="trtitle">异常状态</td>';
            // $html .= '<td class="trcontent">' . $company['statuslistname'] . '</td>';
            // $html .= '</tr>';

            $html .= '</table>';


            $html .= '<table  align="center" style="width=100%; border:1px solid #333;"  valign="middle">';

            $html .= '<tr  align="center">';
            $html .= '<td class="trid">序</td>';
            $html .= '<td class="thtitle" style="line-height:40px;">' . $check_desc . ' </td>';
            if ($checkReport['types'] != 1) {
                $html .= '<td class="thdescyh1" style="line-height:40px;">' . $check_yh . '</td>';
                $html .= '<td class="thdescyh1" style="line-height:40px;">' . $check_zg . '</td>';
            } else {
                $html .= '<td class="thdescyh2" style="line-height:40px;">' . $check_yh . '</td>';
            }

            $html .= '</tr>';

            foreach ($dangerList as $k => $v) {
                $html .= '<tr>';
                $html .= '<td class="trid">' . ($k + 1) . '</td>';
                $html .= '<td class="thtitle"  align="left">' . $v->topicname . '<br/><span class="red">描述：' . $v->desc . '</span></td>';

                if ($checkReport['types'] != 1) {
                    $images = explode(',', $v['images']);
                    $html .= '<td class="thdescyh1">';
                    foreach ($images as $img => $vimg) {
                        $html .= '<img src="' . $vimg . '" class="detailimg" />';
                    }
                    $html .= '</td>';
                    if (empty($v['picture'])) {
                        $html .= '<td class="thdescyh1"></td>';
                    } else {
                        $picture = explode(',', $v['picture']);
                        $html .= '<td class="thdescyh1">';
                        foreach ($picture as $img => $vimgpicture) {
                            $html .= '<img src="' . $vimgpicture . '" class="detailimg" />';
                        }
                        $html .= '</td>';
                    }
                } else {
                    $images = explode(',', $v['images']);
                    $html .= '<td class="thdescyh2">';
                    foreach ($images as $img => $vimg) {
                        $html .= '<img src="' . $vimg . '" class="detailimg" />';
                    }
                    $html .= '</td>';
                }
                $html .= '</tr>';
            }
            $html .= '</table>';
            $pdf->SetFont('stsongstdlight', '', 14);

            $pdf->writeHTMLCell(0, 0, 10, 12, $html, 0, 1, 0, true, 'L', true);

            $pdf->AddPage();

            $htmlf = '
             <style>
               .yhtitle{width:20%;height:40px;line-height:40px;border-bottom:1px solid #333; border-right:1px solid #333;}
               .yhnr{width:30%;height:40px;line-height:40px;border-bottom:1px solid #333; border-right:1px solid #333;}      .yhnote{width:80%;line-height:40px;border-bottom:1px solid #333; border-right:1px solid #333;}
            </style>';
            $htmlf .= '<table  align="center" style="width=100%; border:1px solid #333;"  valign="middle">';
            $htmlf .= '<tr>';
            $htmlf .= '<td class="yhtitle">备注</td>';
            $htmlf .= '<td class="yhnote">' . $company['note'] . '</td>';
            $htmlf .= '</tr>';
            $htmlf .= '</table>';
            $htmlf .= '<table  align="center" style="width=100%; border:1px solid #333;"  valign="middle">';

            $htmlf .= '<tr>';
            $htmlf .= '<td class="yhtitle">检查人员签名</td>';
            $htmlf .= '<td class="yhnr"><img src="' . $sign . '" style="width:60px; height:50px; display:block;"/></td>';
            $htmlf .= '<td class="yhtitle">被查单位确认</td>';
            $htmlf .= '<td class="yhnr"><img src="' . $companySign . '" style="width:60px; height:50px;  display:block;"/></td>';
            $htmlf .= '</tr>';

            $htmlf .= '</table>';
            $pdf->SetFont('stsongstdlight', '', 14);
            $pdf->writeHTMLCell(0, 0, 10, 12, $htmlf, 0, 1, 0, true, 'L', true);
            ob_clean();
            $time = time();
            $find = CheckReport::where(['id' => $param['id']])->find();
            if (!empty($find['pdfurl'])) {
                $pdf_path = $_SERVER['DOCUMENT_ROOT'] . $find['pdfurl'];
                if (file_exists($pdf_path)) {
                    unlink('.' . $find['pdfurl']);
                }
                $pdf_url_path = $find['pdfurl'];
            } else {
                $pdf_path = $_SERVER['DOCUMENT_ROOT'] . '/file/' . $time . $checkReport['check_list_id'] . '.pdf';
                $pdf_url_path = '/file/' . $time . $checkReport['check_list_id'] . '.pdf';

                CheckReport::where(['id' => $param['id']])->update(array('pdfurl' => $pdf_url_path));
            }
            $pdf->Output($pdf_path, 'F');
            return ['code' => 1, 'msg' => '转成PDF成功！', 'url' => $pdf_url_path];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
